﻿Public Class frmVirtualController
    Private m_controller As VirtualController

    Private m_mousePos As PointF
    Private m_leftButton As Boolean
    Private m_rightButton As Boolean

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        m_controller = New VirtualController(Me)

        m_mousePos = New PointF(0.0, 0.0)
    End Sub

    Public ReadOnly Property Controller() As IController
        Get
            Return m_controller
        End Get
    End Property

    Private Sub picLeftStick_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picLeftStick.Paint
        With e.Graphics
            .Clear(Color.White)

            Dim pt As PointF = m_controller.GetLeftStickPos()

            .DrawEllipse(Pens.Black, New Rectangle(pt.X * picLeftStick.Width - 5, pt.Y * picLeftStick.Height - 5, 10, 10))
        End With
    End Sub

    Private Sub picRightStick_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picRightStick.Paint
        With e.Graphics
            .Clear(Color.White)

            Dim pt As PointF = m_controller.GetRightStickPos()

            .DrawEllipse(Pens.Black, New Rectangle(pt.X * picLeftStick.Width - 5, pt.Y * picLeftStick.Height - 5, 10, 10))
        End With
    End Sub

    Private Sub picStick_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picLeftStick.MouseDown, picLeftStick.MouseMove, picRightStick.MouseDown, picRightStick.MouseMove
        If (e.Button And Windows.Forms.MouseButtons.Left) = 0 Then
            Return
        End If

        Dim senderIsLeft As Boolean = (sender Is picLeftStick)

        Dim pt As PointF = New PointF(e.X / 100.0, e.Y / 100.0)

        If pt.X < 0.0 Then pt.X = 0.0
        If pt.X > 1.0 Then pt.X = 1.0
        If pt.Y < 0.0 Then pt.Y = 0.0
        If pt.Y > 1.0 Then pt.Y = 1.0

        If senderIsLeft Then
            m_controller.SetLeftStickPos(pt)
            picLeftStick.Refresh()
        Else
            m_controller.SetRightStickPos(pt)
            picRightStick.Refresh()
        End If
    End Sub

    Private Sub picVirtualScreen_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picVirtualScreen.Paint
        With e.Graphics
            .Clear(Color.LightBlue)

            Dim state As Integer = 0

            If m_leftButton Then
                state += 1
            End If

            If m_rightButton Then
                state += 2
            End If

            Select Case state
                Case 0
                    .FillEllipse(Brushes.WhiteSmoke, New Rectangle(m_mousePos.X - 5, m_mousePos.Y - 5, 10, 10))
                Case 1
                    .FillEllipse(Brushes.Green, New Rectangle(m_mousePos.X - 5, m_mousePos.Y - 5, 10, 10))
                Case 2
                    .FillEllipse(Brushes.Red, New Rectangle(m_mousePos.X - 5, m_mousePos.Y - 5, 10, 10))
                Case 3
                    .FillEllipse(Brushes.Yellow, New Rectangle(m_mousePos.X - 5, m_mousePos.Y - 5, 10, 10))
            End Select

            .DrawEllipse(Pens.Black, New Rectangle(m_mousePos.X - 5, m_mousePos.Y - 5, 10, 10))
        End With
    End Sub

    Public Property LeftButton() As Boolean
        Get
            Return m_leftButton
        End Get
        Set(ByVal value As Boolean)
            m_leftButton = value
        End Set
    End Property

    Public Property RightButton() As Boolean
        Get
            Return m_rightButton
        End Get
        Set(ByVal value As Boolean)
            m_rightButton = value
        End Set
    End Property

    Friend Function VirtMoveMouse(ByVal pt As System.Drawing.PointF) As Boolean
        Dim overflow As Boolean = False

        m_mousePos = New PointF(m_mousePos.X + pt.X, m_mousePos.Y + pt.Y)

        If m_mousePos.X < 0.0 Then
            m_mousePos.X = 0.0
            overflow = True
        End If

        Dim max As Double

        max = picVirtualScreen.Width * 1.0
        If m_mousePos.X > max Then
            m_mousePos.X = max
            overflow = True
        End If

        If m_mousePos.Y < 0.0 Then
            m_mousePos.Y = 0.0
            overflow = True
        End If

        max = picVirtualScreen.Height * 1.0
        If m_mousePos.Y > max Then
            m_mousePos.Y = max
            overflow = True
        End If

        picVirtualScreen.Refresh()

        Return overflow
    End Function
End Class